package com.google.enterprise.gcs.search;

import java.util.ArrayList;
import com.google.enterprise.gcs.search.R;
import android.app.Activity;
import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import android.widget.ImageView;
import android.widget.TextView;

public class ItemAdapter extends ArrayAdapter<ListItem> {
	
    private ArrayList<ListItem> itemDetailsrrayList;
    private Activity activity;
    public ImageManager imageManager;	

	public ItemAdapter(Activity a, int textViewResourceID, ArrayList<ListItem> results) {
	    super(a, textViewResourceID, results);
		itemDetailsrrayList = results;
		activity = a;
        imageManager =  new ImageManager(activity.getApplicationContext());
	}

	@Override
  public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (convertView == null) {
            LayoutInflater vi =  (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);		  
			convertView = vi.inflate(R.layout.listitem, null);
			holder = new ViewHolder();
			holder.txt_itemTitle = (TextView) convertView.findViewById(R.id.title);
			holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price);
			holder.image_itemPhoto = (ImageView) convertView.findViewById(R.id.photo);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ListItem item = itemDetailsrrayList.get(position);
		
		if (item != null) {
  		  holder.txt_itemTitle.setText(item.getTitle());
  		  holder.txt_itemPrice.setText(item.getPrice());
          holder.image_itemPhoto.setTag(item.getImageURL());
          imageManager.displayImage(item.getImageURL(), activity, holder.image_itemPhoto);
		}
		return convertView;
	}

	static class ViewHolder {
		TextView txt_itemTitle;
		TextView txt_itemPrice;
		ImageView image_itemPhoto;
	}
	
}
